// pages/address_administration/address_administration.js 
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        chose:0,
        defaultIndex:-1,
        deleteIndex:-1,
        addressList:[
            {
                id:'0',
                name:'刘大帅',
                phone:'15665597072',
                address:'安徽省合肥市望江路和肥西路交叉口西湖国际广场',
                isDefault:false,
            },
            {
                id: '1',
                name: '刘大帅',
                phone: '15665597073',
                address: '安徽省合肥市望江路和肥西路交叉口西湖国际广场',
                isDefault: false,
            },
            {
                id: '2',
                name: '刘大帅',
                phone: '15665597074',
                address: '安徽省合肥市望江路和肥西路交叉口西湖国际广场交叉口西湖国际广场',
                isDefault:false,
            },
            {
                id: '3',
                name: '刘大帅',
                phone: '15665597075',
                address: '安徽省合肥市望江路和肥西路交叉口西湖国际广场',
                isDefault:false,
            },
            {
                id: '4',
                name: '刘大帅',
                phone: '15665597076',
                address: '安徽省合肥市望江路和肥西路交叉口西湖国际广场',
                isDefault: false,
            },
        ],
    },
    //选择默认地址
    chooseDefaultIndex:function(e){
        var index=e.currentTarget.id;
        var id = e.currentTarget.dataset.id;
        var that=this;
        var addressList = that.data.addressList;
        if (addressList[index].isDefault==false){
            addressList.forEach(item=>{
                item.isDefault=false;
            })
            addressList[index].isDefault=true;
        }else{
            addressList[index].isDefault=false;
        }
        wx.request({
          url: app.siteInfo.shoppath + 'setDefault',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            id: id,
          },
          success: function (res) {
            if (res.data.code == 200) {
              that.setData({
                defaultIndex: index,
                addressList: addressList,
              })
            }
          }
        })
       
    },
    //删除
    delete1:function(e){
        var index=e.currentTarget.id;
        var that=this;
        that.setData({
            deleteIndex:index,
        })
        var addressList = that.data.addressList;
        wx.showModal({
            title: '',
            content: '确定要删除吗?',
            showCancel: true,
            success: function (res) {
                if (res.cancel) {
                    that.setData({
                        deleteIndex:-1,
                    })
                } else {
                    var id = addressList[that.data.deleteIndex].id;
                    wx.request({
                      url: app.siteInfo.shoppath + 'deladdress',
                      method: "post",
                      data: {
                        uid: app.siteInfo.uid,
                        id:id,
                      },
                      success: function (res) {
                        if(res.data.code == 200){
                          addressList.splice(that.data.deleteIndex, 1);
                          that.setData({
                            addressList: addressList,
                          })
                          wx.showToast({
                            title: '已删除',
                          })
                        }else{
                          wx.showToast({
                            title: '删除失败',
                            icon:none
                          })
                        }
                      }
                    })
                    
                }
            }
        })
    },
    linkEdito:function(e){
        var that=this;
        var index=e.currentTarget.id;
        var id = e.currentTarget.dataset.id;
        var addressList = that.data.addressList;
        // var  id= addressList[index].id;
        wx.navigateTo({
            url: '/pages/address_edito/address_edito?id='+id,
        })
    },
    linkAddress:function(){
        wx.navigateTo({
            url:"/pages/add_address/add_address"
        })
    },
    toback:function(e){
      var that = this
      var id = e.currentTarget.dataset.id
      var flag = that.data.chose;
      console.log(flag)
      if(flag == 1){
        wx.redirectTo({
          url: '/pages/order_sure/order_sure?source='+that.data.source+'&ids='+that.data.ids+"&addrid="+id,
        }) 
      }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(options);
      var that = this;
      var flag = options.flag;
      var ids = options.ids;
      var source = options.source;
      if(flag == 'chose'){
        that.setData({
          chose:1,
          ids: ids,
          source: source
        })
      }
    },
    // 获取formid
    getFormId: function (e) {
      var formid = e.detail.formId;
      console.log(formid);
      wx.request({
        url: app.siteInfo.siteroot + 'saveformid',
        method: "post",
        data: {
          uid: app.siteInfo.uid,
          formid: formid
        },
        success: function (res) {

        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this;
      wx.request({
        url: app.siteInfo.shoppath + 'chooseAddress',
        method: "post",
        data: {
          uid: app.siteInfo.uid,
        },
        success: function (res) {
          that.setData({
            addressList: res.data.list
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})